Todd DiLullo

# Assignment 8

output: html_document: pdf_document: default word_document: default title: “Assignment 8: Visualization - Making a bar race” —

How to do it?:

  • Open the Rmarkdown file of this assignment (link) in Rstudio.

  • Right under each question, insert a code chunk (you can use the hotkey Ctrl + Alt + I to add a code chunk) and code the solution for the question.

  • Knit the rmarkdown file (hotkey: Ctrl + Alt + K) to export an html.

  • Publish the html file to your Githiub Page.

Submission: Submit the link on Github of the assignment to Canvas


  1. Install two packages gganimate and gifski then restart Rstudio. Using the Adult Census Income data, make an animation using geom_point and transition_states.
#install.packages('gifski')
library(gganimate)
library(gifski)
library(ggplot2)
library(tidyverse)
library(knitr)
df <- read_csv('https://bryantstats.github.io/math421/data/adult_census.csv')
g <- ggplot(df, 
       aes(x = capital.loss, 
           y = hours.per.week))+
  geom_point(size=4) + 
  transition_states(income)+
labs(title = 'income: {closest_state}')
animate(g)

  1. Using the Adult Census Income data, make an animation using geom_bar and transition_states.
df %>% ggplot(aes(x = income,
                  fill=sex))+
  geom_bar(position = 'fill')+
  transition_states(education) +
  labs(title = 'education: {closest_state}')

  1. Use the WHO’s dataset at this link. Make a top-10 bar race by months between countries on the number of deaths by Covid 19 in 2021.
df <- read_csv('https://covid19.who.int/WHO-COVID-19-global-data.csv')
library(lubridate)
df$Country <- str_replace(df$Country, "United Kingdom of Great Britain and Northern Ireland", "UK and NI")
df$months <- month(df$Date_reported)
d1 <- df %>% group_by(months, Country) %>% summarise(mean = mean(Cumulative_deaths))
d2 <- d1 %>% group_by(months) %>% mutate(rank=rank(-mean)) 
d3 <- d2 %>% filter(rank <= 10)

a1 <- d3 %>% ggplot(aes(x=rank, y=mean, group=Country, fill=Country, label=Country)) + geom_col()+
    geom_text(aes(y = mean, label = Country), hjust = 1.4)+ 
    coord_flip(clip = "off", expand = FALSE) +scale_x_reverse()+
    labs(title = 'month {closest_state}', x='', y='Cumulative Deaths', fill='Country')+
    theme(plot.title = element_text(hjust = 1, size = 22),
          axis.ticks.y = element_blank(),
          axis.text.y  = element_blank()) + 
    transition_states(months)+
    ease_aes("cubic-in-out")
animate(a1, nframes = 400)

  1. Make a bar race using a dataset of your own interest. You may use the dataset that we use in class (https://covidtracking.com/data/download/all-states-history.csv) but you should make a different bar racev from ones in the slides.
df <- read_csv('https://covidtracking.com/data/download/all-states-history.csv')
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
##   dat <- vroom(...)
##   problems(dat)
df <- df %>% filter(deathIncrease>0, positiveIncrease>0)
library(lubridate)
df$months <- months(df$date)
d1 <- df %>% group_by(months, state) %>% summarise(mean = mean(death))
d2 <- d1 %>% group_by(months) %>% mutate(rank=rank(-mean)) 
d3 <- d2 %>% filter(rank <= 10)
a1 <- d3 %>% ggplot(aes(x=rank, y=mean, group=state, fill=state, label=state)) + geom_col()+
    geom_text(aes(y = mean, label = state), hjust = 1.4)+ 
    coord_flip(clip = "off", expand = FALSE) +scale_x_reverse()+
    labs(title = 'month {closest_state}', x='', y='Total Number of Deaths', fill='state')+
    theme(plot.title = element_text(hjust = 1, size = 22),
          axis.ticks.y = element_blank(),
          axis.text.y  = element_blank()) + 
    transition_states(months)+
    ease_aes("cubic-in-out")
animate(a1, nframes = 400)